package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysRole;
import com.easondongh.system.mapper.SysRoleMapper;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.vo.PermissionQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysPermissionMapper;
import com.easondongh.system.domain.SysPermission;
import com.easondongh.system.service.SysPermissionService;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/5/9 14:30
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService{

    @Autowired
    private SysPermissionMapper permissionMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public PageResult<SysPermission> getByPage(BasePageQueryVo<PermissionQueryVo> queryCondition) {
        IPage<SysPermission> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        PermissionQueryVo queryParams = queryCondition.getQueryParams();
        if(queryParams != null) {
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getName()), SysPermission.COL_NAME, queryParams.getName());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getModule()), SysPermission.COL_MODULE, queryParams.getModule());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getOperationType()), SysPermission.COL_OPERATION_TYPE, queryParams.getOperationType());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getPermissionUri()), SysPermission.COL_PERMISSION_URI, queryParams.getPermissionUri());
        }
        queryWrapper.orderByAsc(SysPermission.COL_MODULE, SysPermission.COL_OPERATION_TYPE, SysPermission.COL_NAME);
        IPage<SysPermission> iPage = this.permissionMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public List<String> getPermissionUriByUid(Integer id) {
        // 根据用户查对应角色
        List<Integer> rids = this.roleMapper.getRidsByUid(id);
        if(!CollectionUtils.isEmpty(rids)) {
            // 过滤掉其中未启用的角色
            QueryWrapper<SysRole> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq(SysRole.COL_ENABLE, Constants.ENABLE);
            roleQueryWrapper.in(SysRole.COL_ID, rids);
            List<SysRole> roles = this.roleMapper.selectList(roleQueryWrapper);
            if(!CollectionUtils.isEmpty(roles)) {
                rids = roles.stream().map(r -> r.getId()).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(rids)) {
                    // 根据rid查询pid
                    List<Integer> pids = this.roleMapper.getPidsByRids(rids);
                    if(!CollectionUtils.isEmpty(pids)) {
                        // 查询对应的pid
                        QueryWrapper<SysPermission> permissionQueryWrapper = new QueryWrapper<>();
                        permissionQueryWrapper.in(SysPermission.COL_ID, pids);
                        List<SysPermission> permissions = this.permissionMapper.selectList(permissionQueryWrapper);
                        if(!CollectionUtils.isEmpty(permissions)) {
                            // 获取对应的permissionUri字段
                            List<String> list = permissions.stream().map(p -> p.getPermissionUri()).collect(Collectors.toList());
                            return list;
                        }
                    }
                }
            }
        }
        return null;
    }
}
